package prefix_sum;

public class productExceptSelf {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] pretab = new int[n];//前缀积
        int[] afttab = new int[n]; //后缀积
        //处理细节
        pretab[0] = 1;
        afttab[n-1] = 1;
        //计算
        for(int i =1;i<=n-1;i++) {
            pretab[i] = pretab[i-1]*nums[i-1];
        }
        for(int j = n-2;j >= 0;j--) {
            afttab[j] = afttab[j+1]*nums[j+1];
        }

        int[] ret = new int[n];//最终结果数组
        for(int i = 0;i < n;i++) {
            ret[i] = pretab[i]*afttab[i];
        }
        return ret;
    }
}
